package leetcode;

/**
 * @author: yfq
 * @date: 2023/3/15 18:09
 * @description:
 */
public class Demo209 {
    public static void main(String[] args) {
        //[2,3,1,2,4,3] [5,1,3,5,10,7,4,9,2,8]
        int[] nums = new int[]{2, 3, 1, 2, 4, 3};
//        int[] nums = new int[]{5, 1, 3, 5, 10, 7, 4, 9, 2, 8};

        System.out.println(minSubArrayLen(7, nums));
    }

    public static int minSubArrayLen(int target, int[] nums) {
        int l = 0, r = 0, min = Integer.MAX_VALUE, sum = 0;

        while (r < nums.length) {
            sum += nums[r++];
            while (sum >= target) {
                min = Math.min(min, r - l);
                sum -= nums[l++];
            }
        }

        return min == Integer.MAX_VALUE ? 0 : min;
    }
}
